package netty.client;

/**
 * Created by luoqifei on 16-12-30.
 */

import java.io.*;
import java.net.Socket;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class JavaSocketClient implements Runnable {
    private Socket socket;//MAKE SOCKET INSTANCE VARIABLE
    public static List<String> test = new ArrayList<String>();
    private static String host = "127.0.0.1";
    private static int port = 7878;
    public static Date sTime;
    public static long messageNum = 0;
    public JavaSocketClient(Socket s) {
        socket = s;//INSTANTIATE THE INSTANCE VARIABLE
    }
    public static String getTextStr() {
        return "652103,652103001,GOLDWIND-1500-2012028,1500-2012028-A1,2015-12-30 00:00:03.654,1.840000,2.240000,527.349976,-0.720000,-0.010000,143.589996,143.589996,143.589996,8712.000000,176.070007,189.279999,5.460000,5.240000,5.360000,5.460000,0.020000,0.000000,0.020000,0.000000,-2.700000,-2.400000,-1.390000,-3.700000,-2.200000,-1.700000,-1.520000,357.179993,354.250000,354.739990,-2.630000,0.000000,0.000000,-8.540000,50.040001,3.410000,0.000000,16.000000,3.790000,-4.000000,0.000000,-2.790000,5.400000,4.800000,3.790000,4.400000,3.900000,4.000000,7.900000,8.500000,7.300000,1.100000,0.500000,1.290000,7.900000,8.500000,7.300000,58.740002,59.270000,58.590000,29.110001,29.580000,29.780001,0.550000,0.640000,-0.050000,5.920000,5.980000,6.010000,0.120000,0.150000,0.190000,24719.679688,24312.140625,388.109985,,15171.179688,10340413.000000,22152.000000,10.060000,401.619995,27.120001,27.030001,26.930000,26.780001,26.680000,26.730000,26.879999,26.780001,26.830000,26.780001,26.879999,27.120001,0.800000,19.600000,-1.700000,-1.890000,-2.200000,7.690000,6.090000,497.320007,-505.190002,2.190000,3.250000,344.190002,0.000000,0.000000,0.000000,0.000000,16.000000,4,600,0,0,0,14618.769531,7746.979980,36.119999,6.390000,7895.089844,159.800003,0.000000,12886.700195,0,0.000000,0.000000,0.000000,0.000000,0.000000,0,0,0,192.649994\n";
    }
    public static List<String> getTextLine() {
        //File f = new File("/home/luoqifei/Downloads/652103001.txt");
        File f = new File("/home/luoqifei/Downloads/652103001.txt");
        List<String> text = new ArrayList<String>();
        BufferedReader bre = null;
        try {
            bre = new BufferedReader(new FileReader(f));
            String a = "";
            while ((a = bre.readLine()) != null) {
                //System.out.println(a);
                text.add(a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bre != null) {
                try {
                    bre.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return text;
    }

    @Override
    public void run()//INHERIT THE RUN METHOD FROM THE Runnable INTERFACE
    {
        test = getTextLine();
        Date date = new Date();
        String serverResponse = null;
        try {
            //Scanner chat = new Scanner(System.in);//GET THE INPUT FROM THE CMD
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            //Scanner in = new Scanner(socket.getInputStream());//GET THE CLIENTS INPUT STREAM (USED TO READ DATA SENT FROM THE SERVER)
            PrintWriter out = new PrintWriter(socket.getOutputStream());//GET THE CLIENTS OUTPUT STREAM (USED TO SEND DATA TO THE SERVER)
            String input = getTextStr();
            for (int k = 0; k < messageNum; k++) {
                //String input = chat.nextLine();    //SET NEW VARIABLE input TO THE VALUE OF WHAT THE CLIENT TYPED IN
                out.print(input);//SEND IT TO THE SERVER
                out.flush();//FLUSH THE STREAM
                System.out.println(k);
                //if (in.hasNext()) {//IF THE SERVER SENT US SOMETHING
                if((serverResponse = in.readLine()) != null)  {
                    //System.out.println(serverResponse);//PRINT IT OUT
                    serverResponse = null;
                } else {
                    System.out.println("did not receive!!!!");
                    return;
                }
            }
            System.out.println((new Date()) + " start time is:" + date);
        } catch (Exception e) {
            e.printStackTrace();//MOST LIKELY WONT BE AN ERROR, GOOD PRACTICE TO CATCH THOUGH
        }
    }

    public static void main(String[] args) throws IOException {
        if(args.length<3){
            System.err.println("use: socket host , port, sendMessage number");
            System.exit(1);
        }
        host = args[0];
        port = Integer.valueOf(args[1]);
        try {
            Socket s = new Socket(host, port);//CONNECT TO THE SERVER
            JavaSocketClient client = new JavaSocketClient(s);//START NEW CLIENT OBJECT
            Thread t = new Thread(client);//INITIATE NEW THREAD
            System.out.println("String message length :" + getTextStr().length());
            messageNum = Integer.valueOf(args[2]);
            System.out.println("we will send  :" + messageNum +" messages after 5 seconds.");
            sTime = new Date();
            Thread.sleep(5000);
            System.out.println("start to send data time  is :" + sTime);
            t.start();//START THREAD
            //Thread t2 = new Thread(client);//INITIATE NEW THREAD
            //t2.start();//START THREAD
        } catch (Exception noServer)//IF DIDNT CONNECT PRINT THAT THEY DIDNT
        {
            System.out.println("The server might not be up at this time.");
            System.out.println("Please try again later.");
        }
    }
}

